


//
//  ZYSignInController.m
//  优学荟管理系统
//
//  Created by edz on 2017/12/9.
//  Copyright © 2017年 星道三好. All rights reserved.
//

#import "ZYSignInController.h"
#import "ZYSignInCell.h"
#import "ZYSignInModel.h"
#define kCell @"cell"
#import "HNStudentSignDetailVC.h"
@interface ZYSignInController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *dataSource;
@property (nonatomic,strong) UIView *allView;
@property (nonatomic,assign) BOOL isTrue;
@property (nonatomic,assign) NSInteger page;

@end

@implementation ZYSignInController

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:ColorRGB(0, 0, 0)}];
    [self.navigationController.navigationBar setBackgroundImage:[LLTools createImageWithColor:ColorRGB(255, 255, 255)] forBarMetrics:(UIBarMetricsDefault)];
}

- (NSMutableArray *)dataSource {
    
    
    if (_dataSource == nil) {
        self.dataSource = [NSMutableArray array];
    }
    
    return _dataSource;
    
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"签到记录";
    [self setUpSuperView];
    [self setUpRefresh];
    UIBarButtonItem *liftItem = [UIBarButtonItem itemWithTargat:self action:@selector(itemAction) image:@"icon_back_student_sign" highImage:@"icon_back_student_sign"];
    
    self.navigationItem.leftBarButtonItem =liftItem;
    
}

- (void)itemAction{
    
    [self.navigationController popViewControllerAnimated:YES];
    
    
    
}

#pragma mark --- 上拉加载和下拉刷新方法的实现
- (void)setUpRefresh{
    
    //下拉刷新
    self.tableView.mj_header  = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(downRefresh)];
    //自动更改透明度
    self.tableView.mj_header.automaticallyChangeAlpha = YES;
    //进入刷新状态
    [self.tableView.mj_header beginRefreshing];
    
    //上拉加载
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(upRefresh)];
    //结束头部刷新
    [self.tableView.mj_header endRefreshing];
    //结束尾部刷新
    [self.tableView.mj_footer endRefreshing];
    
    
}

/**
 *  上拉加载
 */
- (void)upRefresh{
    
    self.isTrue = NO;
    self.page++;
    [self setNetWork];
    
}
/**
 *  下拉刷新
 */

- (void)downRefresh{
    self.isTrue = YES;
    self.page = 1;
    [self setNetWork];
    
    
}




- (void)setUpSuperView{
    
  
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 64)];
    self.tableView.dataSource =self;
    self.tableView.delegate = self;
    self.tableView.backgroundColor = ColorRGB(247, 245, 245);
    [self.view addSubview:self.tableView];
    [self.tableView registerClass:[ZYSignInCell class] forCellReuseIdentifier:kCell];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

    self.tableView.estimatedRowHeight = 200;
    self.tableView.contentInset = UIEdgeInsetsMake(-54, 0, 0, 0);
    
    self.allView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenHeight, kScreenHeight *30/1334)];
    self.allView.backgroundColor = ColorRGB(247, 245, 245);
    self.tableView.tableHeaderView = self.allView;
 
    [self setNetWork];
}




- (void)setNetWork{
    
//    http://192.168.0.113:8080/StarShine/ws/trainLesson.do?action=getPushInfoList&pageNumber=1&enrollId=0
    NSString *url = [NSString stringWithFormat:@"trainLesson.do?action=getPushInfoList&pageNumber=%ld&enrollId=%@",(long)self.page,self.ID];
    
    
    NSLog(@"%@   %@",LLBaseURLs(url),[AccountTool shareAccountTool].currentAccount.authCode);
    
    [SVProgressHUD setDefaultStyle:(SVProgressHUDStyleCustom)];
    [SVProgressHUD  setBackgroundColor:ColorRGB(4, 4, 4)];
    [SVProgressHUD setForegroundColor:[UIColor whiteColor]];
    [SVProgressHUD showWithStatus:LLLoading];
    
    
    [NetManager Get:LLBaseURLs(url) Auth:YES Params:nil Success:^(id Obj) {
        
        [SVProgressHUD dismiss];
        
        if ([[Obj valueForKey:@"code"] integerValue] == 1000) {
            if (self.isTrue == YES) {
                [self.dataSource removeAllObjects];
                
            }
            NSDictionary *t = [Obj valueForKey:@"t"];
            
            NSArray *arr = [t valueForKey:@"dataList"];
            

            
            if (arr != nil && ![arr isKindOfClass:[NSNull class]] && arr.count != 0) {
                for (NSDictionary *dict in arr) {
                    
                    ZYSignInModel *model = [ZYSignInModel new];
                    
                    [model setValuesForKeysWithDictionary:dict];
                    
                    
                    [self.dataSource addObject:model];
                    
                    
                }
            }
            
            
            
            
            [self.tableView reloadData];
            [self.tableView.mj_header endRefreshing];
            [self.tableView.mj_footer endRefreshing];
            
            
            
        }
        
        
        
    } Fail:^(NSError *Error) {
        [self.tableView.mj_footer endRefreshing];
        [self.tableView.mj_header endRefreshing];
        [SVProgressHUD dismiss];
    }];
    
    
    
    
    
    
    
    
    
}




















#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataSource.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ZYSignInCell *cell = [tableView dequeueReusableCellWithIdentifier:kCell forIndexPath:indexPath];
  
    
    if (self.dataSource.count > indexPath.row) {
        
        ZYSignInModel *model = self.dataSource[indexPath.row];
        
        [cell assignByZYSignInModel:model];
        
    }
    
     cell.selectionStyle = UITableViewCellSeparatorStyleNone;
    
    return cell;
}





- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    ZYSignInModel *model = [self.dataSource objectAtIndex:indexPath.row];
    
    HNStudentSignDetailVC *VC = [[HNStudentSignDetailVC alloc] init];
    VC.ID       = model.ID;
    VC.Status   = @([model.status integerValue]);
    VC.Time     = [[model.recordTime componentsSeparatedByString:@" "] firstObject];
    [self.navigationController pushViewController:VC animated:YES];
    
    
    
    
}











- (void)viewWillDisappear:(BOOL)animated {
    
    [super viewWillDisappear:animated];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
    [self.navigationController.navigationBar setBackgroundImage:[LLTools createImageWithColor:ColorRGB(47, 204, 138)] forBarMetrics:(UIBarMetricsDefault)];;

}











@end
